package com.mall.order.services;

import com.mall.commons.tool.exception.BizException;
import com.mall.order.OrderPromoService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.CreateSeckillOrderRequest;
import com.mall.order.dto.CreateSeckillOrderResponse;
import com.mall.order.mq.delayOrder.DelayOrderProducer;
import com.mall.order.utils.GlobalIdGeneratorUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.ProductDetailDto;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;

/**
 * Author  kkw
 * Date  2021/6/25 15:27
 * Version 1.0
 * Description :秒杀业务生成订单
 */
@Service
@Slf4j
public class OrderPromoServiceImpl implements OrderPromoService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    DelayOrderProducer delayOrderProducer;

    @Reference
    IProductService productService;


    @Override
    @Transactional
    public CreateSeckillOrderResponse createPromoOrder(CreateSeckillOrderRequest request) {
        CreateSeckillOrderResponse createSeckillOrderResponse = new CreateSeckillOrderResponse();

        //插入订单表
        String orderId= null;
        try {
            Order order = new Order();
            orderId = null;
            try {
                GlobalIdGeneratorUtil globalIdGeneratorUtil = new GlobalIdGeneratorUtil();
                orderId= globalIdGeneratorUtil.getNextSeq(request.getUsername(),request.getUserId().intValue());
            } catch (IOException e) {
                throw new BizException("005094","生成订单号失败");
            }
            order.setOrderId(orderId);
            order.setUserId(request.getUserId());
            order.setBuyerNick(request.getUsername());
            order.setPayment(request.getPrice());
            order.setUpdateTime(new Date());
            order.setBuyerMessage("秒杀业务");
            order.setCreateTime(new Date());
            order.setStatus(OrderConstants.ORDER_STATUS_INIT);
            int insert = orderMapper.insert(order);
            if (insert < 1) {
                throw new BizException(OrderRetCode.DB_SAVE_EXCEPTION.getCode(),OrderRetCode.DB_EXCEPTION.getMessage());
            }
            //插入商品关联表
            //根据商品Id获取商品详情
            Long productId = request.getProductId();
            ProductDetailRequest productDetailRequest = new ProductDetailRequest(productId);
            ProductDetailDto productDetailDto = productService.getProductDetail(productDetailRequest).getProductDetailDto();

            OrderItem orderItem = new OrderItem();
            orderItem.setId(orderId);
            orderItem.setItemId(productId);
            orderItem.setOrderId(orderId);
            orderItem.setNum(1);
            orderItem.setTitle(productDetailDto.getProductName());
            orderItem.setPrice(request.getPrice().doubleValue());
            orderItem.setTotalFee(request.getPrice().doubleValue());
            orderItem.setPicPath(productDetailDto.getProductImageBig());
            orderItem.setStatus(3);
            int insert1 = orderItemMapper.insert(orderItem);
            if (insert1 < 1) {
                throw new BizException(OrderRetCode.DB_SAVE_EXCEPTION.getCode(),OrderRetCode.DB_EXCEPTION.getMessage());
            }
            //插入商品寄送地址
            OrderShipping orderShipping = new OrderShipping();
            orderShipping.setOrderId(orderId);
            orderShipping.setReceiverName(request.getUsername());
            orderShipping.setReceiverAddress(request.getStreetName());
            orderShipping.setReceiverPhone(request.getTel());
            orderShipping.setCreated(new Date());
            orderShipping.setUpdated(new Date());
            int insert3 = orderShippingMapper.insert(orderShipping);
            if (insert3 < 1) {
                throw new BizException(OrderRetCode.SHIPPING_DB_SAVED_FAILED.getCode(),OrderRetCode.SHIPPING_DB_SAVED_FAILED.getMessage());
            }
        } catch (BizException e) {
            createSeckillOrderResponse.setCode(OrderRetCode.DB_SAVE_EXCEPTION.getCode());
            createSeckillOrderResponse.setMsg(OrderRetCode.DB_SAVE_EXCEPTION.getMessage());
            return createSeckillOrderResponse;
        }

        //发送延迟取消订单的消息
        delayOrderProducer.sendDelayOrderMsg(orderId);
        createSeckillOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
        createSeckillOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
//        System.out.println(createSeckillOrderResponse);
        return createSeckillOrderResponse;
    }
}
